#include "Acceptor.hpp"
#include "TcpConnection.hpp"
#include "EventLoop.hpp"

#include <iostream>

using std::cout;
using std::endl;

void onConnection(wd::TcpConnectionPtr conn)
{
    cout << conn -> toString() << "连接成功！" << endl;
}

void onMessage(wd::TcpConnectionPtr conn)
{
    // read
    string msg = conn -> recieve();
    cout << "recv: " << msg << endl;

    // decode
    // compute
    // encode

    // 假设执行的回显服务
    string response = msg;

    // send
    conn -> send(response);
}

void onClose(wd::TcpConnectionPtr conn)
{
    cout << conn -> toString() << "已断开链接！" << endl; 
}

int main()
{
    wd::Acceptor acceptor(9090);
    acceptor.ready();

    wd::EventLoop loop(acceptor);
    loop.setAllCallbacks(
                         onConnection , 
                         onMessage , 
                         onClose
                        );

    loop.loop();

    return 0;
}
